package com.cjy.string;

/**
 * @author Cheng JiYe
 * @date 2020/3/19 22:17
 */
public class StringUtils {
    /**
     * 是否为null或空字符串
     *
     * @param val
     * @return
     */
    public static boolean isBlank(Object val) {
        if (val == null) {
            return true;
        } else if (val instanceof String) {
            return isBlank(val.toString());
        }

        return false;
    }

    /**
     * 是否不为null或空字符串
     *
     * @param val
     * @return
     */
    public static boolean isNotBlank(Object val) {
        return !isBlank(val);
    }

    /**
     * 判断字符串是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }


    /**
     * 判断字符串不为空
     *
     * @param cs
     * @return
     */
    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

    /**
     * 判断字符串是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符串不为空
     *
     * @param cs
     * @return
     */
    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }


    /**
     * 默认字符串
     *
     * @param val
     * @return
     */
    public static String defaultString(Object val) {
        return val == null ? "" : val.toString();
    }

    /**
     * 字符串转ASCII
     *
     * @param value
     * @return
     */
    public static String stringToAscii(String value) {
        StringBuffer sbu = new StringBuffer();
        char[] chars = value.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i != chars.length - 1) {
                sbu.append((int) chars[i]).append(",");
            } else {
                sbu.append((int) chars[i]);
            }
        }
        return sbu.toString();
    }

    /**
     * 默认字符串
     *
     * @param val
     * @return
     */
    public static String defaultStringNull(Object val) {
        return val == null ? null : val.toString();
    }


}
